<?php

namespace Home\Controller;

class JudgeController extends BaseController{

	/*
		审判管理-案件管理
	 */
	public function caseManagement(){
		if(IS_POST){
			// var_dump($_POST);die;
			$id = $_POST['id']; // 考核年份记录表
			$notes = M('notes')->where('id='.$id)->find();
			$year = date('Y',$notes['ktime']);
			$time1 = $year . '-' . '12';
			$time = strtotime($time1) + 2678399;
			$lian_time = strtotime($_POST['lian_time']); // 立案时间
			$jiean_time = strtotime($_POST['jiean_time']); // 结案时间
			$ca_type = $_POST['ca_type']; // 案件类别
			$team = $_POST['team']; // 承办庭室
			$worker = $_POST['worker']; // 承办人
			if(!empty($lian_time)){
				$search['ktime'] = array('elt',$lian_time);
			}else{
				$search['ktime'] = array('elt',$time);
			}
			if(!empty($jiean_time)){
				$search['stime'] = array('elt',$jiean_time);
			}
			if(!empty($ca_type)){
				$search['case_type'] = array('like',$ca_type);
			}
			if(!empty($team)){
				$search['team'] = array('like',$team);
			}
			if(!empty($worker)){
				$search['worker'] = array('like',$worker);
			}
			$m = M('channel');
			$p = getpage($m,$search,'50');
			$list = $m->field(true)->where($search)->order('ktime asc')->select();
			$this->page = $p->show();
			$this->assign('list',$list);
			$this->display('y_search');
		}elseif(!empty($_GET['id'])){
			// var_dump($_GET);die;
			$id = $_GET['id'];
			$notes = M('notes')->where('id='.$id)->find();
			$ktime = $notes['ktime'];
			$year = date('Y',$notes['ktime']);
			$time1 = $year . '-' . '12';
			$time = strtotime($time1) + 2678399;
			$mp['ktime'] = array('elt',$time);
			if(!empty($_GET['lian_time'])){
				$lian_time = strtotime($_GET['lian_time']);
				$map['ktime'] = array('elt',$lian_time);
			}else{
				
				$map['ktime'] = array('elt',$time);
			}
			if(!empty($_GET['jiean_time'])){
				$jiean_time = strtotime($_GET['jiean_time']);
				$map['stime'] = array('elt',$jiean_time);
			}
			if(!empty($_GET['ca_type'])){
				$map['case_type'] = array('like',$_GET['ca_type']);
			}
			if(!empty($_GET['team'])){
				$map['team'] = array('like',$_GET['team']);
			}
			if(!empty($_GET['worker'])){
				$map['worker'] = array('like',$_GET['worker']);
			}
			// 分页显示
			$m = M('channel');
			$p = getpage($m,$map,'50');
			$list = $m->field(true)->where($map)->order('ktime asc')->select();
			$list1 = M('channel')->where($mp)->order('ktime asc')->select();
			foreach($list as $k => $v){
				if($v['stime'] > $time){
					$list[$k]['stime'] = '';
				}
			}
			foreach($list1 as $k1 => $v1){
				$ca_type1[] = $v1['case_type'];
				$team1[] = $v1['team'];
				$worker1[] = $v1['worker'];
			}
			$ca_type = array_unique($ca_type1);
			$team = array_unique($team1);
			$worker = array_unique($worker1);
			$notes1 = M('notes')->order('ktime desc')->select();
			$this->page = $p->show();
			$this->assign('list',$list);
			$this->assign('notes',$notes1);
			$this->assign('ktime',$ktime);
			$this->assign('lian_time',$_GET['lian_time']);
			$this->assign('jiean_time',$_GET['jiean_time']);
			$this->assign('sca_type',$_GET['ca_type']);
			$this->assign('steam',$_GET['team']);
			$this->assign('sworker',$_GET['worker']);
			$this->assign('ca_type',$ca_type); // 案件类别
			$this->assign('team',$team); // 承办庭室
			$this->assign('worker',$worker); // 承办人
			$this->display('y_search1');
			
		}else{
			$notes = M('notes')->order('ktime desc')->select();
			$year = date('Y',$notes['0']['ktime']);
			$time1 = $year . '-' . '12';
			$time = strtotime($time1) + 2678399;
			// 默认显示所有的数据
			$map['ktime'] = array('elt',$time);
			$list1 = M('channel')->where($map)->order('ktime asc')->select();
			//分页显示
			$m = M('channel');
			$p = getpage($m,$map,'50');
			$list = $m->field(true)->where($map)->order('ktime asc')->select();
			foreach($list as $k => $v){
				if($v['stime'] > $time){
					$list[$k]['stime'] = '';
				}
			}
			foreach($list1 as $k1 => $v1){
				$ca_type1[] = $v1['case_type'];
				$team1[] = $v1['team'];
				$worker1[] = $v1['worker'];
			}
			$ca_type = array_unique($ca_type1);
			$team = array_unique($team1);
			$worker = array_unique($worker1);
			$this->page = $p->show();
			$this->assign('list',$list);
			$this->assign('notes',$notes);
			$this->assign('ca_type',$ca_type); // 案件类别
			$this->assign('team',$team); // 承办庭室
			$this->assign('worker',$worker); // 承办人



			//图表分析
			$notestt = M('notes')->order('ktime desc')->select();
			$yeartt = date('Y',$notestt['0']['ktime']);
			$time1tt = $yeartt . '-' . '12';
			$timett = strtotime($time1tt) + 2678399;
			$maptt['ktime'] = array('elt',$timett);
			$listtt = M('channel')->where($maptt)->order('ktime asc')->select();
			foreach($listtt as $k => $v){
				if($v['stime'] > $timett){
					$listtt[$k]['stime'] = '';
				}
			}
			$counttt = count($listtt);
			// 板块1 开始
			foreach($listtt as $k => $v){
				$casetype1[] = $v['case_type'];
				if(!empty($v['stime'])){
					$list1tt[] = $v['id']; // 已结案件
				}else{
					$list2tt[] = $v['id']; // 未结案件
				}
			}
			// 已结案件所占比例（所有）
			$yj1 = round(count($list1tt) / $counttt,3) * 100;
			// 未结案件所占比例（所有）
			$wj1 = round(count($list2tt) / $counttt,3) * 100;
			// 去除相同的案件类型
			$casetype = array_unique($casetype1);
			foreach($casetype as $k1 => $v1){
				$map1tt['case_type'] = array('like',$v1);
				$case1[] = M('channel')->where($map1tt)->select();
			}
			foreach($case1 as $k2 => $v2){
				$num[$k2] = count($v2); // 各类案件数量
				foreach($v2 as $k4 => $v4){
					if(!empty($v4['stime'])){
						$sum1[$k2][] = $v4['id']; // 各类案件已结案件
					}else{
						$sum2[$k2][] = $v4['id']; // 各类案件未结案件
					}
				}
			}
			foreach($num as $k3 => $v3){
				// 各类案件所占比例
				$num1[$k3] = round($v3 / $counttt,3) * 100;
			}
			foreach($sum1 as $k5 => $v5){
				$su1[$k5] = count($v5);
			}
			foreach($su1 as $k7 => $v7){
				foreach($num as $k9 => $v9){
					if($k7 == $k9){
						// 各类案件已结案件所占比例
						$s1[$k7] = round($v7 / $v9,3) * 100; 
					}
				}
			}
			foreach($sum2 as $k6 => $v6){
				$su2[$k6] = count($v6);
			}
			foreach($su2 as $k8 => $v8){
				foreach($num as $k10 => $v10){
					if($k8 == $k10){
						// 各类案件未结案件所占比例
						$s2[$k8] = round($v8 / $v10,3) * 100;
					}
				}
			}
			// 案件类型
			$type1 = implode(',',$casetype);
			// 各类案件所占比例
			$num2 = implode(',',$num1);
			// 各类案件已结案件占本案件类型比例
			$yj2 = implode(',',$s1);
			// 各类案件未结案件占本案件类型比例
			$wj2 = implode(',',$s2);
			// 各类案件已结案件占总案件数比例
			$yj4 = implode(',',$yj3);
			// 各类案件未结案件占总案件数比例
			$wj4 = implode(',',$wj3);
			// 传$type1、$num2、$yj1、$wj1、$yj2、$wj2、$yj4、$wj4到页面
			// 案件类型
			$this->assign('type1',$type1);
			// 各类案件所占比例
			$this->assign('num2',$num2);
			// 已结案件所占比例（所有）
			$this->assign('yj1',$yj1);
			// 未结案件所占比例（所有）
			$this->assign('wj1',$wj1);
			// 各类案件已结案件占本案件类型比例
			$this->assign('yj2',$yj2);
			// 各类案件未结案件占本案件类型比例
			$this->assign('wj2',$wj2);
			// 板块1 结束	
			$this->assign('notestt',$notestt);

			$this->display();
		}
	}

	/*
		审判管理-案件管理按年份查看案件ajax请求
	 */
	public function y_first(){
		// var_dump($_POST);die;
		if(IS_POST){
			$id = $_POST['id'];
			$notes = M('notes')->where('id='.$id)->find();
			$year = date('Y',$notes['ktime']);
			$time1 = $year . '-' . '12';
			$time = strtotime($time1) + 2678399;
			$map['ktime'] = array('elt',$time);
			// 分页显示
			$m = M('channel');
			$p = getpage($m,$map,'50');
			$list = $m->field(true)->where($map)->order('ktime asc')->select();
			foreach($list as $k => $v){
				if($v['stime'] > $time){
					$list[$k]['stime'] = '';
				}
			}
			$this->page = $p->show();
			$this->assign('list',$list);
			$this->display();
		}else{
			$id = $_GET['id'];
			$notes = M('notes')->where('id='.$id)->find();
			$ktime = $notes['ktime'];
			$year = date('Y',$notes['ktime']);
			$time1 = $year . '-' . '12';
			$time = strtotime($time1) + 2678399;
			$map['ktime'] = array('elt',$time);
			// 分页显示
			$m = M('channel');
			$p = getpage($m,$map,'50');
			$list = $m->field(true)->where($map)->order('ktime asc')->select();
			foreach($list as $k => $v){
				if($v['stime'] > $time){
					$list[$k]['stime'] = '';
				}
			}
			$notes1 = M('notes')->order('ktime desc')->select();
			$this->page = $p->show();
			$this->assign('list',$list);
			$this->assign('notes',$notes1);
			$this->assign('ktime',$ktime);
			$this->display('y_first1');
		}
		
	}

	/*
		审判管理-案件分析板块一 年份改变ajax请求
	 */
	public function y_one(){
		// var_dump($_POST);die;
		$id = $_POST['id'];
		$notes = M('notes')->where('id='.$id)->find();
		$year = date('Y',$notes['ktime']);
		$time1 = $year . '-' . '12';
		$time = strtotime($time1) + 2678399;
		$map['ktime'] = array('elt',$time);
		$list = M('channel')->where($map)->order('ktime asc')->select();
		$count = count($list);
		foreach($list as $k => $v){
			$casetype1[] = $v['case_type'];
			if($v['stime'] > $time){
				$list[$k]['stime'] = '';
			}
		}
		foreach($list as $k1 => $v1){
			if(!empty($v1['stime'])){
				$list1[] = $v1['id']; // 已结案件
			}else{
				$list2[] = $v1['id']; // 未结案件
			}
		}

		// 已结案件所占比例（所有）
		$yj1 = round(count($list1) / $count,3) * 100;
		// 未结案件所占比例（所有）
		$wj1 = round(count($list2) / $count,3) * 100;
		// 去除相同的案件类型
		$casetype = array_unique($casetype1);
		foreach($casetype as $k1 => $v1){
			foreach($list as $k2 => $v2){
				if($v1 == $v2['case_type']){
					$res[$k1][] = $v2;
				}
			}
		}
		foreach($res as $k3 => $v3){
			$num[$k3] = count($v3);
			foreach($v3 as $k4 => $v4){
				if(!empty($v4['stime'])){
					$sum1[$k3][] = $v4['id']; // 各类案件已结案件
				}else{
					$sum2[$k3][] = $v4['id']; // 各类案件未结案件
				}
			}
		}
		foreach($num as $k3 => $v3){
			// 各类案件所占比例
			$num1[$k3] = round($v3 / $count,3) * 100;
		}
		foreach($sum1 as $k5 => $v5){
			$su1[$k5] = count($v5);
		}
		foreach($su1 as $k7 => $v7){
			foreach($num as $k9 => $v9){
				if($k7 == $k9){
					// 各类案件已结案件所占比例
					$s1[$k7] = round($v7 / $v9,3) * 100; 
				}
			}
		}
		foreach($sum2 as $k6 => $v6){
			$su2[$k6] = count($v6);
		}
		foreach($su2 as $k8 => $v8){
			foreach($num as $k10 => $v10){
				if($k8 == $k10){
					// 各类案件未结案件所占比例
					$s2[$k8] = round($v8 / $v10,3) * 100;
				}
			}
		}
		if(empty($s1)){
			foreach($s2 as $k => $v){
				if($v == 100){
					$s1[$k] = 0;
				}
			}
		}elseif(empty($s2)){
			foreach($s1 as $k1 => $v1){
				if($v1 == 100){
					$s2[$k1] = 0;
				}
			}
		}
		// 案件类型
		$type1 = implode(',',$casetype);
		// 各类案件所占比例
		$num2 = implode(',',$num1);
		// 各类案件已结案件占本案件类型比例
		$yj2 = implode(',',$s1);
		// 各类案件未结案件占本案件类型比例
		$wj2 = implode(',',$s2);
		// 各类案件已结案件占总案件数比例
		$yj4 = implode(',',$yj3);
		// 各类案件未结案件占总案件数比例
		$wj4 = implode(',',$wj3);
		// 传$type1、$num2、$yj1、$wj1、$yj2、$wj2、$yj4、$wj4到页面
		// 案件类型
		$this->assign('type1',$type1);
		// 各类案件所占比例
		$this->assign('num2',$num2);
		// 已结案件所占比例（所有）
		$this->assign('yj1',$yj1);
		// 未结案件所占比例（所有）
		$this->assign('wj1',$wj1);
		// 各类案件已结案件占本案件类型比例
		$this->assign('yj2',$yj2);
		// 各类案件未结案件占本案件类型比例
		$this->assign('wj2',$wj2);
		$this->display();
	}	
}